`
https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var longestSubarray = function (nums) {
  // 等价于求最长的只含一个 0 的子数组 len ，返回为 len - 1
  // 如果数组全都是 1，随便删掉一个即可，返回 n - 1
  const n = nums.length
  let res = 0
  let cnt = 0
  let left = 0, right = 0

  while (right < n) {
    const n1 = nums[right++]
    if (n1 === 0) cnt++

    // 更新答案
    if (cnt === 1) res = Math.max(res, right - left)

    // 缩小窗口
    while (cnt > 1) {
      const n2 = nums[left++]
      if (n2 === 0) cnt--
    }
  }

  return res !== 0 ? res - 1 : n - 1
};